﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace Server.Controllers
{
    public class FileController : ApiController
    {
        private string filePath = @"G:\XLNetAccSetup.exe";
        

        [HttpGet]
        [HttpPost]
        public IHttpActionResult Download()
        {
            var browser = String.Empty;
            if (HttpContext.Current.Request.UserAgent != null)
            {
                browser = HttpContext.Current.Request.UserAgent.ToUpper();
            }

            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK);

            FileStream fileStream = File.OpenRead(filePath);
            httpResponseMessage.Content = new StreamContent(fileStream);
            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            httpResponseMessage.Headers.AcceptRanges.Add("bytes");
            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = browser.Contains("FIREFOX") ? Path.GetFileName(filePath) : HttpUtility.UrlEncode(Path.GetFileName(filePath))
            };
            return ResponseMessage(httpResponseMessage);
        }
        [HttpGet]
        [HttpPost]
        public IHttpActionResult Download2()
        {

            var range = Request.Headers.Range?.Ranges?.FirstOrDefault();
            if (range == null) return Download();

            var browser = String.Empty;
            if (HttpContext.Current.Request.UserAgent != null)
            {
                browser = HttpContext.Current.Request.UserAgent.ToUpper();
            }

            HttpResponseMessage httpResponseMessage = new HttpResponseMessage(HttpStatusCode.PartialContent);
            FileStream fileStream = File.Open(filePath,FileMode.Open, FileAccess.Read, FileShare.Read);
            long fromIndex = range.From.Value;
            long toIndex = 0;

            if (range.To == null )
            {
                toIndex = fromIndex + 60000;
                if (toIndex > fileStream.Length-1) toIndex = fileStream.Length-1;
            }
            else
            {
                toIndex = range.To.Value;
            }
            var count = fileStream.Length;
            var memoryStream = new MemoryStream();
            var buffer = new byte[toIndex- fromIndex+1];
            using (fileStream)
            {
                fileStream.Seek(fromIndex, SeekOrigin.Begin);
                fileStream.Read(buffer, 0, buffer.Length);
                memoryStream.Write(buffer, 0, buffer.Length);
                memoryStream.Position = 0;
                httpResponseMessage.Content = new StreamContent(memoryStream);
            }

            //httpResponseMessage.Content = new StreamContent(fileStream,80*1024);
            httpResponseMessage.Content.Headers.ContentRange = new ContentRangeHeaderValue(fromIndex, toIndex, count);
            httpResponseMessage.Content.Headers.ContentLength = memoryStream.Length;
            httpResponseMessage.Headers.AcceptRanges.Add("bytes");
            httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = browser.Contains("FIREFOX") ? Path.GetFileName(filePath) : HttpUtility.UrlEncode(Path.GetFileName(filePath))
            };
            return ResponseMessage(httpResponseMessage);

        }

        
    }
}
